Skip to content

Fix jpg_quality/jpeg_quality field mismatch with OCRmyPDF >=17.10 - #63

Open
tah0 wants to merge 1 commit into
kcroker:masterfrom
tah0:fix/ocrmypdf-jpeg-quality-rename
Open

Fix jpg_quality/jpeg_quality field mismatch with OCRmyPDF >=17.10#63
tah0 wants to merge 1 commit into
kcroker:masterfrom
tah0:fix/ocrmypdf-jpeg-quality-rename

Conversation

@tah0

@tah0 tah0 commented Aug 10, 2026

Copy link
Copy Markdown

Any optimize level (-O<n>) crashes on OCRmyPDF >=17.10.0 with:

pydantic_core._pydantic_core.ValidationError: 1 validation error for OcrOptions
jpg_quality
  Extra inputs are not permitted [type=extra_forbidden, input_value=0, input_type=int]

Reproduction

Any djvu file will do, e.g.:

wget -q -O sample.djvu "https://upload.wikimedia.org/wikipedia/commons/8/8d/Example_for_DjVu_manual_cz-book_color.djvu"

The following crashes with the error above

uv run --with dpsprep --with 'ocrmypdf>=17.10' dpsprep -O1 sample.djvu sample.pdf

Cause

src/dpsprep/ocrmypdf_adapter.py expects the _options.OcrOptions field jpg_quality, which was renamed to jpeg_quality in OCRmyPDF 17.10.0 (6f4744dd). dpsprep constructs OcrOptions directly instead of going through ocrmypdf.ocr(), which calls create_options() to remap deprecated parameter names. So it isn't caught here.

Fix

In ocrmypdf_adapter.py, detect which field name the installed OCRmyPDF's OcrOptions has and use that:

jpg_quality_key = 'jpeg_quality' if 'jpeg_quality' in OcrOptions.model_fields else 'jpg_quality'
...
omp_options = OcrOptions(
    ...,
    png_quality=quality or 0,
    **{jpg_quality_key: quality or 0},
)

Works across both old 17.8.1 (pre-rename) and new >=17.10 (post-rename) OCRmyPDF versions.

Notes

  • Unrelated and not addressed here: rerunning on the same input after a failed run reuses a stale /var/tmp/dpsprep/ working dir and hangs. But to rerun dpsprep on the same input after a failure (like when testing for this error) you need to delete the old working dir first.

…tect

which name is used to work with old and new versions of OCRmyPDF.

OcrOptions field rename in 17.10 causes dpsprep to crash with a pydantic
ValidationError when calling OCRmyPDF optimization (-O<n> flag).
@v--

v-- commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

Hello. Thank you for the fix. I'm afraid OCRmyPDF have entered their automated "move fast and break things" phase, so other things may break soon. Using the private OcrOptions module (which is "New style (recommended)" in the docs) was already bad enough; perhaps I must propose some new changes to their public API.

Anyhow, one of the positive changes from them was to introduce type hints. I we un-ignore ocrmypdf from mypy scanning (i.e. if we remove "ocrmypdf.*" from the end of pyproject.toml), your fix leads to a type error.

One way around this is to use the backwards-compatible setter, i.e. to construct omp_options without jpg_quality and to later add

omp_options.jpg_quality = quality or 0

If you do this fix, you can also commit the change in pyproject.toml I mentioned.

Now, regarding your comment

rerunning on the same input after a failed run reuses a stale /var/tmp/dpsprep/ working dir and hangs

I believe I found the issue independently and will now do a fix. We create this temporary directory so that it can be reused it in case of an error, so deleting it automatically defeats its purpose. There is a setting, -d or --delete-working that should force deletion of a pre-existing working directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants